<template>
  <div class="goods">
    <div class="wrap">
      <Crumb> </Crumb>
      <img src="../assets/img/banner.4c6b6225.png" width="100%" alt="" />
      <ul class="options">
        <li>
          <strong>排序：</strong>
          <span
            :class="{ active: index == options1Num }"
            v-for="(item, index) in options1"
            :key="index"
            @click="options1Fn(index, item.min, item.max)"
            >{{ item.txt }}</span
          >
          <!-- <span>我还可以兑换</span> -->
          <!-- <span>0-500个</span> -->
          <!-- <span>500-1000个</span> -->
          <!-- <span>1000-1500个</span> -->
          <!-- <span>1500-2500个</span> -->
        </li>
        <li>
          <strong>分类：</strong>
          <span
            :class="{ active: item.isActive }"
            v-for="(item, index) in options2"
            :key="index"
            @click="options2Fn(index, item.type)"
            >{{ item.txt }}</span
          >
          <!-- <span>实物礼品</span> -->
          <!-- <span>虚拟礼品</span> -->
        </li>
      </ul>
      <List :arr="goodListShow" />
      <div style="text-align: center">
        {{ isReachBottom ? "已经没有数据了" : "正在加载中..." }}
      </div>
    </div>
  </div>
</template>
<script>
import Crumb from "../components/Crumb.vue";
import List from "../components/home/List.vue";
import { GoodsSearchApi } from "@/request/api";
import { getScrollTop, getClientHeight, getScrollHeight } from "@/utils";
export default {
  mounted() {
    // console.log("添加监听*****");
    window.addEventListener("scroll", this.scrollFn);
  },
  beforeDestroy() {
    // console.log("取消监听");
    window.removeEventListener("scroll", this.scrollFn);
  },
  created() {
    // let res = await GoodsSearchApi({
    // did: this.did,
    // type: this.type,
    // min: this.min,
    // max: this.max,
    // keyword: this.keyword,
    // });
    // console.log("全部商品筛选", res);
    // this.goodList = res.data;
    // console.log(this.$route.query.keyword);
    this.keyword = this.$route.query.keyword || "";
    this.getGoodsSearchApi();
  },
  data() {
    return {
      did: 0,
      type: 0,
      min: 0,
      max: 0,
      keyword: "",
      goodList: [],
      goodListShow: [],
      // 商品列表
      options1: [
        { min: 0, max: 10000, txt: "全部" },
        // "我还可以兑换"的max值是Header组件的coin值
        { min: 0, max: 0, txt: "我还可以兑换" },
        { min: 0, max: 500, txt: "0-500分" },
        { min: 500, max: 1000, txt: "500-1000分" },
        { min: 1000, max: 1500, txt: "1000-1500分" },
        { min: 1500, max: 2500, txt: "1500-2500分" },
        { min: 2500, max: 6500, txt: "2500-6500分" },
        { min: 6500, max: 10000, txt: "6500-10000分" },
      ],
      options1Num: 0,
      options2: [
        { type: 0, txt: "全部", isActive: true },
        { type: 1, txt: "实体商品", isActive: false },
        { type: 2, txt: "虚拟商品", isActive: false },
      ],
      page: 1,
      size: 8,
      // 是否正在加载
      isLoading: false,
      isReachBottom: false,
    };
  },
  components: {
    Crumb,
    List,
  },
  watch: {
    "$route.query.keyword": {
      handler(newVal, oldVal) {
        if (newVal != oldVal) {
          this.keyword = newVal;
          this.getGoodsSearchApi();
        }
      },
    },
  },
  methods: {
    scrollFn() {
      // console.log("滚动了",);
      if (getScrollTop() + getClientHeight() >= getScrollHeight() - 50) {
        // console.log("要加载数据了");

        if (this.goodListShow.length >= this.goodList.length) {
          this.isReachBottom = true;
          return;
        }

        if (this.isLoading == false) {
          this.isLoading = true;
          this.page++;

          setTimeout(() => {
            for (
              var i = this.size * (this.page - 1);
              i < this.size * this.page;
              i++
            ) {
              console.log("往show添加的数据", this.page, this.goodList[i]);
              this.goodList[i] && this.goodListShow.push(this.goodList[i]);
            }
            this.isLoading = false;
          }, 1000);
        }
      }
    },
    async getGoodsSearchApi() {
      let res = await GoodsSearchApi({
        did: this.did,
        type: this.type,
        min: this.min,
        max: this.max,
        keyword: this.keyword,
      });
      // console.log("全部商品筛选", res);
      this.goodList = res.data;
      this.goodListShow = this.goodList.filter((item, index) => {
        return index < 8;
      });
      this.page = 1;
      this.isReachBottom = false;
      if (this.goodListShow.length >= this.goodList.length) {
        this.isReachBottom = true;
      }
    },
    async options1Fn(index, min, max) {
      this.options1Num = index;
      this.min = min;
      this.max = max;
      // let res = await GoodsSearchApi({
      // did: this.did,
      // type: this.type,
      // min: this.min,
      // max: this.max,
      // keyword: this.keyword,
      // });
      // console.log("全部商品筛选", res);
      // this.goodList = res.data;
      this.getGoodsSearchApi();
    },
    async options2Fn(index, type) {
      this.options2.forEach((item) => {
        item.isActive = false;
      });
      this.options2[index].isActive = true;
      this.type = type;
      // let res = await GoodsSearchApi({
      // did: this.did,
      // type: this.type,
      // min: this.min,
      // max: this.max,
      // keyword: this.keyword,
      // });
      // console.log("全部商品筛选", res);
      // this.goodList = res.data;
      this.getGoodsSearchApi();
    },
  },
};
</script>

<style lang="less" scoped>
@import "../assets/css/public.less";

.goods {
  padding-bottom: 50px;
  background: #efefef;
  padding-top: 20px;
  .options {
    padding-top: 20px;
    padding-bottom: 40px;
    li {
      margin-top: 20px;
      strong {
        color: #000;
        font-weight: bold;
      }
      span {
        margin-right: 20px;
        margin-left: 10px;
        cursor: pointer;
        color: #999;
        &.active {
          color: @base-color;
          font-weight: bold;
        }
      }
    }
  }
}
</style>
